$(document).ready(function() {
	
	
	/*****Detect browsers and change it's css accordingly****/
	
	//If IE 8
	if(jQuery.browser.version == "8.0") {
		$("a#logo").prev().css("height", "10px");
		
	}
	//If IE 7
	if(jQuery.browser.version == "7.0") {
		$("div#search").css("position", "relative");
		$("div#search").css("right", "20px");
	}
	//If IE 6
	if(jQuery.browser.version == "6.0") {
		
	}
	
	
	/********************************************************/
	
	
	
	$.ajaxSetup ({

		// Disable caching of AJAX responses */
		cache: false
	});
		
	/*
	 * Inputs of type text
	 */
	
	//$('input[type="text"]').addClass("idleField");
	$('input[type="text"]').focus(function() {
		//$(this).removeClass("idleField").addClass("focusField");
	    if (this.value == this.defaultValue){ 
	    	this.value = '';
		}
		if(this.value != this.defaultValue){
			this.select();
		}
	});
	$('input[type="text"]').blur(function() {
		//$(this).removeClass("focusField").addClass("idleField");
	    if ($.trim(this.value) == ''){
	    	this.value = (this.defaultValue ? this.defaultValue : '');
		}
	    
	});
	
	$("#login_btn").live("click", function (){
		var username_txt = $("#username").val();
		var password_txt = $("#password").val();
		username_txt = username_txt.replace(/^\s*|\s*$/,"");
		password_txt = password_txt.replace(/^\s*|\s*$/,"");
		
		$("#login_loader").show();
		
		//$.post("/us/en/ajax/login", { username: username_txt, password: password_txt  }, function(data){
			
		$("div#header").load('/default/ajax/login?username='+username_txt+'&password='+password_txt, function(){
			$("#login_loader").hide();	
		});
			
		//});		
		
		return false;
	});
	
	$("#logout_btn").live("click", function (){
		$("#login_loader").show();

		$("div#header").load('/default/ajax/logout', function(){
			$("#login_loader").hide();	
		});
		
		//turn off the edit mode if it was on
		$("span#edit_btn_toggle").text("Off");
		$(".edit_off_mode").removeClass('edit_on_mode');
		
	});	
	
	
	// market place and language selectors // needs to be moved to a seperate js file
	$("#country_select").bind("focus", function (){		
		
		$("div#country_list").css("display:inline-block");
		$("div#country_list").show();

	});
	
	$("#country_select").bind("blur", function (){		
		$("div#country_list").hide();

	});
	
	$("div.cl_element").bind("mouseenter", function(){
		$("#country_select").unbind("blur");
		
	});
	
	$("div#country_list").bind("mouseleave", function(){
		$("#country_select").bind("blur", function (){		
			$("div#country_list").hide();

		});
		
	});	

	$("div.cl_element").bind("click", function(){
		var country = $(this).text();
		var country_size = (country.length>7) ? country.length : 7;
		var box_width = country_size*10+"px";
		$("input#country_select").css({width:box_width});
		$("input#country_select").val(country);
		$("div#country_list").hide();
			
	});	
	
	/****************************************************************************/
	
	$("#language_select").bind("focus", function (){		
		
		$("div#language_list").css("display:inline-block");
		$("div#language_list").show();

	});

	$("#language_select").bind("blur", function (){		
		$("div#language_list").hide();

	});	
	
	$("div.ll_element").bind("mouseenter", function(){
		$("#language_select").unbind("blur");
		
	});
	
	$("div#language_list").bind("mouseleave", function(){
		$("#language_select").bind("blur", function (){		
			$("div#language_list").hide();

		});
		
	});	

	$("div.ll_element").bind("click", function(){
		var country = $(this).text();
		var country_size = (country.length>7) ? country.length : 7;
		var box_width = country_size*10+"px";
		$("input#language_select").css({width:box_width});
		$("input#language_select").val(country);
		$("div#language_list").hide();
			
	});
	$("li[id^='country_code_']").click(function(){
		
		var country_code = $(this).attr('id').substr(13);
		var country = $(this).text();
		var country_id = $(this).attr('country_id');
		
		var sURL = unescape(window.location.pathname);
	
		$('body').css('cursor', 'wait'); 
		$.post("/default/ajax/setlocale", { country: country, country_code: country_code, country_id: country_id  }, function(data){
			window.location.href = sURL;
		});
		return false;
	});
	
	$("li[id^='language_code_']").click(function(){
		var language_code = $(this).attr('id').substr(14);
		var language = $(this).text();
		var language_id = $(this).attr('lang_id');
		
		var sURL = unescape(window.location.pathname);
		
		$('body').css('cursor', 'wait');
		$.post("/default/ajax/setlocale", { language: language, language_code: language_code, language_id: language_id  }, function(data){
			window.location.href = sURL;
		});
		return false;
	});
	
	/********************** Trans Login ***********************/

	
	$("a#login_link").click(function(){
		var login_popup = '';
		login_popup += '<div id="login_box">';

		login_popup += '<table>';
		login_popup += '<tr>';
		login_popup += '<td>Username:</td><td><input type="text" id="tran_username" /></td>';
		login_popup += '</tr>';
		login_popup += '<tr>';
		login_popup += '<td>Password:</td><td><input type="password" id="tran_password"/></td>';
		login_popup += '</tr>';
		login_popup += '</table>';
		login_popup += '<input type="submit" value="Login" onclick="translator_login()" />';	
		login_popup += '</div>';
		
		$("div#new_content").html(login_popup);
		$("div#login_box").dropShadow({left: 2, top: 2, blur: 1, color: "#000"});
	
	});
	

	
});

function translator_login()
{
	var href = "/default/ajax/login?username="+$("input#tran_username").val()+"&password="+$("input#tran_password").val();
	document.location.href = href;
}